<!--  -->
<template>
  <div id="main">
    <h2>分支机构管理</h2>

    <el-button type="primary" style="margin:20px" @click="()=>{handleAdd()}">添加分支机构</el-button>

    <div id="search-card">
      <div id="search-content">
        <el-form v-model="search" :inline="true">

          <el-form-item label="分支机构名称">
            <el-input v-model="search.campusName" />
          </el-form-item>

          <el-form-item label="省">
            <el-input v-model="search.provice" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="conditionQuery">查找</el-button>
          </el-form-item>

          <el-form-item label="模糊查询">
            <el-input v-model="mohu" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="mohuQuery">查找</el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <!-- <el-table-column
          align="center"
          prop="campusId"
          label="校区编号"
          min-width="5%"
        /> -->

        <el-table-column
          label="序号"
          type="index"
          min-width="5%"
          align="center"
        />

        <el-table-column
          align="center"
          prop="campusName"
          label="分支机构名称"
          min-width="18%"
        />

        <!-- <el-table-column
          align="center"
          prop="campusImg"
          label="校区logo"
          min-width="10%"
        >
          <template slot-scope="Scope">
            <img :src="Scope.row.campusImg" width="100px" height="60px">
          </template>
        </el-table-column> -->

        <el-table-column
          align="center"
          prop="provice"
          label="地区"
          min-width="6%"
        />

        <el-table-column
          align="center"
          prop="org_username"
          label="负责人"
          min-width="6%"
        />

        <el-table-column
          align="center"
          prop="conntact"
          label="联系人"
          min-width="6%"
        />

        <el-table-column
          align="center"
          prop="contactNumber"
          label="联系电话"
          min-width="10%"
        />

        <el-table-column
          align="center"
          label="二维码"
          min-width="10%"
        >
          <template slot-scope="Scope">
            <vue-qr :text="String(Scope.row.campusId)" :size="60" :margin="0" />
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="campusState"
          label="当前状态"
          min-width="5%"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.campusState==0" style="color:red">未上线</span>
            <span v-if="Scope.row.campusState==1" style="color:green">已上线</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" min-width="30%">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleEdit(Scope.row.campusId)">编辑信息</el-button>
            <el-button type="primary" icon="el-icon-edit" @click="statusVisible=true;id=Scope.row.campusId">编辑状态</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.campusId,Scope.row.campusName)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <detail :id="id" :visible="editVisible" :type="type" :title="title" @close="handleClose" />

    <el-dialog title="修改分支机构状态" :visible.sync="statusVisible">
      <el-form :model="status">
        <el-form-item label="状态：">
          <el-select v-model="status" placeholder="请选择">
            <el-option key="0" label="未上线" value="0" />
            <el-option key="1" label="已上线" value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleUpdateStatus">修 改</el-button>
        <el-button @click="statusVisible=false">返 回</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import VueQr from 'vue-qr'
import { updateStatusById, deleteById, conditionSearch, mohuSearch, getCampusByOrg } from '@/api/campus'
import { getOrgByOrgHead } from '@/api/org'
import Detail from '@/views/campus/detail'
export default {
  name: 'MyCampus',
  // import引入的组件需要注入到对象中才能使用
  components: {
    'detail': Detail,
    VueQr
  },
  data() {
    // 这里存放数据
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      listLoading: false,
      dialogFormVisible: false,
      pwdFormVisible: false,
      orgId: '',
      consoleUser: {},
      changePwd: {},
      id: 0,
      editVisible: false,
      statusVisible: false,
      type: 0,
      title: '',
      status: '',
      search: {},
      mohu: ''
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      getOrgByOrgHead().then((resp) => {
        this.orgId = resp.obj.orgId
        this.listLoading = true
        getCampusByOrg(this.orgId).then((data) => {
          this.list = data.obj
          this.listLoading = false
        })
      })
    },
    // generateQRCode() {
    //   this.text = String(this.campus.campusId)
    // },
    handleAdd() {
      this.title = '添加分支机构'
      this.type = 1
      this.editVisible = true
    },
    handleClose() {
      this.editVisible = false
    },
    handleEdit(id) {
      this.id = id
      this.title = '编辑分支机构'
      this.type = 1
      this.editVisible = true
      this.type = 2
    },
    handleUpdateStatus() {
      console.log(this.id, this.status)
      updateStatusById(this.id, this.status).then(() => {
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        this.fetchData()
      })
      this.statusVisible = false
    },
    handleDelete(id, name) {
      this.$confirm('确认删除该分支机构？分支机构名称为:' + name).then(() => deleteById(id).then((data) => {
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        const totalPage = Math.ceil((this.list.length - 1) / this.pagesize)
        const page = this.currentPage > totalPage ? totalPage : this.currentPage
        this.currentPage = this.currentPage < 1 ? 1 : page
        this.fetchData()
      }))
    },
    conditionQuery() {
      console.log(this.search)
      conditionSearch(this.search).then((data) => {
        this.list = data.obj
        this.search = {}
        this.currentPage = 1
      })
    },
    mohuQuery() {
      console.log(this.mohu)
      mohuSearch(this.mohu).then((data) => {
        this.list = data.obj
        this.currentPage = 1
      })
    }

  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style  scoped>
    #main{
        margin-left: 5px;
        margin-right: 5px;
    }

    #table-area{

        margin-left: 10px;
        margin-right: 10px;
    }
#search-card {
  margin-left: 50px;
  width: 100%;
  height: 100px;
  overflow-x: hidden;
}
</style>
